#!/bin/sh
# PCP QA Test No. 1433
# pmproxy rc script stop/start with timeseries option.
#
# Copyright (c) 2019 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_key_server
eval `$PCP_BINADM_DIR/pmconfig -L ipv6 unix_domain_sockets`
[ "$ipv6" = "true" ] || _notrun "No IPv6 support"
[ "$unix_domain_sockets" = "true" ] || _notrun "No Unix socket support"

_cleanup()
{
    cd $here
    _restore_config $PCP_PMPROXYOPTIONS_PATH
    if $pmproxy_was_running
    then
	echo "Restart pmproxy ..." >>$seq_full
	_service pmproxy restart >>$seq_full 2>&1
	_wait_for_pmproxy
    else
	echo "Stopping pmproxy ..." >>$seq_full
	_service pmproxy stop >>$seq_full 2>&1
	_wait_pmproxy_end
    fi
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

pmproxy_was_running=false
[ -f $PCP_RUN_DIR/pmproxy.pid ] && pmproxy_was_running=true
echo "pmproxy_was_running=$pmproxy_was_running" >>$seq_full

# real QA test starts here
_save_config $PCP_PMPROXYOPTIONS_PATH

echo "# Option added by PCP QA test $seq" > $tmp.local
echo --timeseries >> $tmp.local
$sudo cp $tmp.local $PCP_PMPROXYOPTIONS_PATH

if ! _service pmproxy stop >/dev/null 2>&1; then _exit 1; fi
_wait_pmproxy_end || _exit 1
# give any running pmproxy a chance to shutdown cleanly
#
sleep 2
if ! _service pmproxy start 2>&1; then _exit 1; fi | _filter_pmproxy_start
_wait_for_pmproxy || _exit 1

if ! _service pmproxy stop 2>&1; then _exit 1; fi | _filter_pmproxy_start
_wait_pmproxy_end || _exit 1
_filter_pmproxy_log < $PCP_LOG_DIR/pmproxy/pmproxy.log

# systemd has a default limits ... systemd.unit(5) describe
# StartLimitIntervalSec and StartLimitBurst but not the default values
# which are hidden in *.conf files below /etc/systemd and appear
# to have defaults like:
#	DefaultStartLimitIntervalSec=10s
#	DefaultStartLimitBurst=5
# ... this test has 2 (re)starts, so we need it to run for 4 seconds
# to avoid hitting the limit
#
sleep 4

# success, all done
status=0
exit
